;**************************************************************************
;*
;* FILE  blitter_scroll.asm 
;* Copyright (c) 2006 Daniel Kahlin <daniel@kahlin.net>
;* Written by Daniel Kahlin <daniel@kahlin.net>
;* $Id: blitter_scroll.asm,v 1.48 2006-08-07 22:57:26 tlr Exp $
;*
;* DESCRIPTION
;*   blitter scroll
;*
;*  layout:
;*
;*  $020000-$040000  screen 1       1 line every 512 bytes.
;*  $040000-$060000  screen 2       1 line every 512 bytes.
;*  $080000-$090000  font           1 char every 256 bytes.
;*  $060000-$080000  master screen  1 line every 512 bytes.
;*
;*
;******
	processor 6502

BORDER_COLOR	equ	$85
BG_COLOR	equ	$85

HAVE_SONG	equ	1
CUSTOM_FONT	equ	0
DEBUG		equ	0
REAL_BG		equ	1
NUM_OBJECTS	equ	55	
;NUM_OBJECTS	equ	70	
;NUM_OBJECTS	equ	110
STEPS	equ	6
YSTEPS	equ	6
; STEPS	equ	4		

OFFSET		equ	8
	
SCREEN_BASE	equ	512*OFFSET+OFFSET
SCREEN1_PAGE	equ	$02
SCREEN2_PAGE	equ	$04
MASTER_PAGE	equ	$06
FONT_PAGE	equ	$08

	
		
	MAC	sac
	dc.b	$32,{1}
	ENDM
	MAC	sir
	dc.b	$42,{1}
	ENDM
	MAC	bra
	dc.b	$12,{1}-2-.
	ENDM
	
; setup segments
	seg.u	zp
	org	$fb
ptr_zp:
	ds.w	1
tmp_zp:
	ds.b	1
c_zp:
	ds.b	1

	seg	code
	org	$0801

;**************************************************************************
;*
;* Sysline
;*
;******
StartOfFile:
	dc.w	EndLine
SumStart:
	dc.w	0
	dc.b	$9e,"2069 /T.L.R/",0
;	        0 SYS2069 /T.L.R/
EndLine:
	dc.w	0
;**************************************************************************
;*
;* Start of the program! 
;*
;******
SysAddress:
	jsr	intro_init
	sei
	lda	#$05
	sta	$01
	lda	#<nmi_entry
	sta	$fffa
	lda	#>nmi_entry
	sta	$fffb
	
; skip internal cycle, burst mode
	sac	#$99
	lda	#%00000011
	sac	#$00

; enable extended registers
	lda	#1
	sta	$d03f
	
sa_sm1:
	jsr	install
	lda	#$2c
	sta	sa_sm1

	jsr	intro_run

	jsr	render_bg
	jsr	clear
	jsr	prepare_font
	jsr	init_text
	lda	#SCREEN1_PAGE
	sta	buf_disp
	jsr	initvideoregs
	lda	#BORDER_COLOR
	sta	$d020

	if	HAVE_SONG
	lda	#0
	jsr	SONG_DEST
	endif	
sa_lp1:
	jsr	wait_wb
	lda	buf_disp
	sta	$d04b	;Ptr B (High)
	eor	#SCREEN1_PAGE^SCREEN2_PAGE
	sta	buf_disp

; swap to a fresh buffer
	jsr	swap

; scan keys and update the current mode
	jsr	set_mode	

; clear all objects
	if	DEBUG
	lda	#$86
 	sta	$d020		
	endif
	jsr	blit_clr

; update objects	
	if	DEBUG
	lda	#$c6
 	sta	$d020
	endif
	jsr	update
		
; render objects
	if	DEBUG
	lda	#$46
 	sta	$d020
	endif
	jsr	blit_render
	if	DEBUG
	lda	#$56
	sta	$d020
	endif

	if	HAVE_SONG
	jsr	SONG_DEST+3
	endif	
	if	DEBUG
	lda	#BORDER_COLOR
	sta	$d020
	endif
	
	
	bra	sa_lp1

nmi_entry:
	rti	


;**************************************************************************
	dc.b	"*** BY TLR 8TH OF AUGUST 2006 ***"
;**************************************************************************

	
;**************************************************************************
;*
;* wait for vertical blanking
;*
;******
wait_wb:
ww_lp1:
	lda	$d011
	bpl	ww_lp1
ww_lp2:
	lda	$d011
	bmi	ww_lp2
	rts

;**************************************************************************
;*
;* Init Video Registers
;*
;******
initvideoregs:
	jsr	wait_wb
	
; Chunky=1, No badlines=1, Color Disable=1,
; Overscan=0, HighColor=1, Border Off=0, Linear=1	
	lda	#%01110101
	sta	$d03c
; ECM=1, BMM=0, Screen=1, 25Row=1, YScroll=3 
	lda	#%01011011
	sta	$d011
; MCM=1, 40Col=1, XScroll=0
	lda	#%00011000
	sta	$d016

; Pointers and modulos
	lda	#512-320
	sta	$d047	;Mod B (Low)
	ldx	#0
	stx	$d048	;Mod B (High)
	ldx	#<[SCREEN_BASE]
	stx	$d049	;Ptr B (Low)
	ldx	#>[SCREEN_BASE]
	stx	$d04a	;Ptr B (Mid)
	ldx	#SCREEN1_PAGE
	stx	$d04b	;Ptr B (High)
	ldx	#8
	stx	$d04c	;Step B

; Pointers and modulos
	ldx	#0
	stx	$d038	;Mod A (Low)
	stx	$d039	;Mod A (High)
	stx	$d03a	;Ptr A (Low)
	stx	$d03b	;Ptr A (Mid)
	ldx	#$00
	stx	$d045	;Ptr A (High)
	ldx	#1
	stx	$d046	;Step A


	jsr	reset_mode
	rts

;**************************************************************************
;*
;* render bg
;*
;******
render_bg:
	ldx	#0
rb_lp1:
	txa
	lsr
	and	#$1f
	cmp	#$10
	bcc	rb_skp1
	eor	#$1f
rb_skp1:
	sta	render_buf,x	
	sta	render_buf+256,x	
	sta	render_buf+256*2,x	
	inx
	bne	rb_lp1

	ldx	#$1e
rb_lp2:
	lda	dmatab_render,x
	sta	$d300,x
	dex
	bpl	rb_lp2


	ldx	#8
rb_lp3:
	stx	$d300
	txa
	asl
	sta	$d304
	lda	#0
	rol
	ora	#MASTER_PAGE | $40
	sta	$d305
	jsr	dorend
	inx
	cpx	#208
	bne	rb_lp3
	rts
	
	
dorend:
	lda	#%00001101
	sta	$d31f
dr_lp1:
	lda	$d31f
	lsr
	bcs	dr_lp1
	rts

dmatab_render:
	dc.b	<render_buf,>render_buf,$40                 ; $d300 src
	dc.b	<SCREEN_BASE,>SCREEN_BASE,MASTER_PAGE | $40 ; $d303 dest
	dc.w	1		; $d306 src step
	dc.w	1		; $d308 dest step
	dc.w	320		; $d30a dma length
	dc.w	0		; $d30c src modulo
	dc.w	0		; $d30e dest modulo
	dc.w	320		; $d310 src line len
	dc.w	320		; $d312 dest line len
	dc.b	0,0,0,0,0,0,0,0,0 ;  unused
	dc.b	0		; $d31d irq
	dc.b	%00000000	; $d31e modulo enables

;**************************************************************************
;*
;* clear screen
;*
;******
clear:
	ldx	#$1e
cl_lp1:
	lda	dmatab_clr,x
	sta	$d300,x
	dex
	bpl	cl_lp1

	lda	#SCREEN1_PAGE | $40
	jsr	cdma
	lda	#SCREEN2_PAGE | $40
cdma:
	sta	$d305
	lda	#%00001101
	sta	$d31f
cl_lp2:
	lda	$d31f
	lsr
	bcs	cl_lp2
	rts

dmatab_clr:
	if	REAL_BG
	dc.b	<SCREEN_BASE,>SCREEN_BASE,MASTER_PAGE | $40 ; $d300 src
	dc.b	<SCREEN_BASE,>SCREEN_BASE,0 ; $d303 dest
	dc.w	1		; $d306 src step
	dc.w	1		; $d308 dest step
; why is 201 lines needed?
	dc.w	320*201		; $d30a dma length
	dc.w	512-320		; $d30c src modulo
	dc.w	512-320		; $d30e dest modulo
	dc.w	320		; $d310 src line len
	dc.w	320		; $d312 dest line len
	dc.b	0,0,0,0,0,0,0,0,0 ;  unused
	dc.b	0		; $d31d irq
	dc.b	%00000011	; $d31e modulo enables
	else
	dc.b	<zdata,>zdata,$40 ; $d300 src
	dc.b	<SCREEN_BASE,>SCREEN_BASE,0 ; $d303 dest
	dc.w	0		; $d306 src step
	dc.w	1		; $d308 dest step
; why is 201 lines needed?
	dc.w	320*201		; $d30a dma length
	dc.w	0		; $d30c src modulo
	dc.w	512-320		; $d30e dest modulo
	dc.w	1		; $d310 src line len
	dc.w	320		; $d312 dest line len
	dc.b	0,0,0,0,0,0,0,0,0 ;  unused
	dc.b	0		; $d31d irq
	dc.b	%00000010	; $d31e modulo enables
zdata:
	dc.b	BG_COLOR
	endif



FONT_START	equ	$d000	
FONT_END	equ	$d400

initdma_font:
	ldx	#$1e
idf_lp1:
	lda	dmatab_font,x
	sta	$d300,x
	dex
	bpl	idf_lp1
	rts

dma_font:
	sta	$d304
	lda	#%00001101
	sta	$d31f
df_lp1:
	lda	$d31f
	lsr
	bcs	df_lp1
	rts

dmatab_font:
	dc.b	<font_temp,>font_temp,$40 ; $d300 src
	dc.b	$00,0,FONT_PAGE | $40	; $d303 dest
	dc.w	1		; $d306 src step
	dc.w	1		; $d308 dest step
	dc.w	8*8*4		; $d30a dma length
	dc.w	0		; $d30c src modulo
	dc.w	0		; $d30e dest modulo
	dc.w	1		; $d310 src line len
	dc.w	1		; $d312 dest line len
	dc.b	0,0,0,0,0,0,0,0,0 ;  unused
	dc.b	0		; $d31d irq
	dc.b	%00000000	; $d31e modulo enables
	
prepare_font:
	jsr	initdma_font
	ldy	#0
	sty	ptr_zp
	sty	c_zp
	lda	#>FONT_START
	sta	ptr_zp+1

pf_lp3:	

	lda	#$03
	sta	$01

	ldy	#0
	ldx	#0
pf_lp1:
	lda	(ptr_zp),y
	sta	tmp_zp
	tya
	pha

	ldy	#7
pf_lp2:
	asl	tmp_zp
	bcc	pf_skp1
	lda	font_fade,x
	sta	font_temp,x
	lda	font_fade2,x
	sta	font_temp+$40,x
	lda	font_fade3,x
	sta	font_temp+$80,x
	lda	#$ff
	sta	font_temp+$c0,x
	bra	pf_skp2
pf_skp1:
	lda	#$00
	sta	font_temp,x
	sta	font_temp+$40,x
	sta	font_temp+$80,x
	sta	font_temp+$c0,x
pf_skp2:
	inx
	dey
	bpl	pf_lp2

	pla
	tay
	iny
	cpy	#8
	bne	pf_lp1

	lda	#$05
	sta	$01

	lda	c_zp
	jsr	dma_font	
	inc	c_zp
		
	lda	ptr_zp
	clc
	adc	#8
	sta	ptr_zp
	bcc	pf_lp3
	inc	ptr_zp+1
	lda	ptr_zp+1
	cmp	#>FONT_END
	bne	pf_lp3
	
	rts


font_fade:
	dc.b	$ff,$fe,$fd,$fc,$fc,$fd,$fe,$ff
	dc.b	$fe,$fd,$fc,$fb,$fb,$fc,$fd,$fe
	dc.b	$fd,$fc,$fb,$fa,$fa,$fb,$fc,$fd
	dc.b	$fc,$fb,$fa,$f9,$f9,$fa,$fb,$fc
	dc.b	$fd,$fc,$fb,$fa,$fa,$fb,$fc,$fd
	dc.b	$fe,$fd,$fc,$fb,$fb,$fc,$fd,$fe
	dc.b	$ff,$fe,$fd,$fc,$fc,$fd,$fe,$ff
	dc.b	$ff,$fe,$fd,$fc,$fc,$fd,$fe,$ff
font_fade2:
	dc.b	$f9,$f9,$f9,$f9,$f9,$f9,$f9,$f9
	dc.b	$fb,$fb,$fb,$fb,$fb,$fb,$fb,$fb
	dc.b	$fd,$fd,$fd,$fd,$fd,$fd,$fd,$fd
	dc.b	$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff
	dc.b	$fd,$fd,$fd,$fd,$fd,$fd,$fd,$fd
	dc.b	$fb,$fb,$fb,$fb,$fb,$fb,$fb,$fb
	dc.b	$f9,$f9,$f9,$f9,$f9,$f9,$f9,$f9
	dc.b	$f7,$f7,$f7,$f7,$f7,$f7,$f7,$f7
font_fade3:
	dc.b	$f8,$f9,$fa,$fb,$fc,$fd,$fe,$ff
	dc.b	$f9,$f8,$f9,$fa,$fb,$fc,$fd,$fe
	dc.b	$fa,$f9,$f8,$f9,$fa,$fb,$fc,$fd
	dc.b	$fb,$fa,$f9,$f8,$f9,$fa,$fb,$fc
	dc.b	$fc,$fb,$fa,$f9,$f8,$f9,$fa,$fb
	dc.b	$fd,$fc,$fb,$fa,$f9,$f8,$f9,$fa
	dc.b	$fe,$fd,$fc,$fb,$fa,$f9,$f8,$f9
	dc.b	$ff,$fe,$fd,$fc,$fb,$fa,$f9,$f8

;**************************************************************************
;*
;* Do blit
;*
;******
blitinit_clr:
	ldx	#$1e
bic_lp1:
	lda	blittab_clr,x
	sta	$d320,x
	dex
	bpl	bic_lp1
	rts


blittab_clr:
	dc.b	$c0,0,FONT_PAGE	; $d320 src A
	dc.w	0		; $d323 mod A
	dc.w	8		; $d325 line len A
	dc.b	$10		; $d327 frac A
	if	REAL_BG
	dc.b	$00,$00,$00	; $d328 src B
	dc.w	512-8		; $d32b mod B
	dc.w	8		; $d32d line len B
	dc.b	$10		; $d32f frac B
	else
	dc.b	<kill,>kill,0	; $d328 src B
	dc.w	0		; $d32b mod B
	dc.w	0		; $d32d line len B
	dc.b	0		; $d32f frac B
	endif
	dc.b	$00,$00,$00	; $d330 dest D
	dc.w	512-8		; $d333 mod D
	dc.w	8		; $d335 line len D
	dc.b	$10		; $d337 frac D
	dc.w	8*8+7		; $d338 blit length
	dc.b	0		; $d33a start
	dc.b	%00000100	; $d33b anti-transparent, disable B
	dc.b	0,0		; not used
	dc.b	%00000000	; $d33e minterms + shift
kill:
	dc.b	BG_COLOR

blitinit_render:
	ldx	#$1e
bi_lp1:
	lda	blittab_render,x
	sta	$d320,x
	dex
	bpl	bi_lp1
	rts
	
blittab_render:
font_offs:
	dc.b	$00,0,FONT_PAGE	; $d320 src A
	dc.w	0		; $d323 mod A
	dc.w	8		; $d325 line len A
	dc.b	$10		; $d327 frac A
	dc.b	<col,>col,0	; $d328 src B
	dc.w	0		; $d32b mod B
	dc.w	0		; $d32d line len B
	dc.b	0		; $d32f frac B
	dc.b	$00,$00,$00	; $d330 dest D
	dc.w	512-8		; $d333 mod D
	dc.w	8		; $d335 line len D
	dc.b	$10		; $d337 frac D
	dc.w	8*8+7		; $d338 blit length
	dc.b	0		; $d33a start
	dc.b	%00000100	; $d33b transparent
	dc.b	0,0		; not used
	dc.b	%00111000	; $d33e minterms + shift

col:
	dc.b	$0f		
		
	align	4
	ds.b	3
blit:
blit_render:
	jsr	blitinit_render
	
	ldx	#NUM_OBJECTS-1
	ldy	#[NUM_OBJECTS-1]*2
blr_lp1:

	lda	cols,x
 	ora	lums,x
	sta	col
	lda	chars,x
	sta	$d321		; src=$YYxx00
blt_sm1:
	sta	d321_a,x

	lda	xpos,y
	sta	$d330
blt_sm2:
	sta	d330_a,x
	lda	ypos,x
	asl
	php
	clc
	adc	xpos+1,y
	sta	$d331
blt_sm3:
	sta	d331_a,x
	plp
	lda	#0
	adc	buf_disp
	sta	$d332
blt_sm4:
	sta	d332_a,x
	
	lda	#%00001111
	sta	$d33a
bl_lp1:
	lda	$d33f
	lsr
	bcs	bl_lp1

	dey
	dey
	dex
	bpl	blr_lp1
	
	rts

	align	4
	ds.b	3
blit_clr:
	jsr	blitinit_clr
	ldx	#NUM_OBJECTS-1
blc_lp1:
blt_sm5:
	lda	d321_a,x
	sta	$d321
blt_sm6:
	lda	d330_a,x
	sta	$d330
	if	REAL_BG
	sta	$d328
	endif
blt_sm7:
	lda	d331_a,x
	sta	$d331
	if	REAL_BG
	sta	$d329
	endif
blt_sm8:
	lda	d332_a,x
	sta	$d332
	if	REAL_BG
	and	#$01
	ora	#MASTER_PAGE
	sta	$d32a
	endif
	lda	#%00001111
	sta	$d33a
blc_lp2:
	lda	$d33f
	lsr
	bcs	blc_lp2

	dex
	bpl	blc_lp1

	rts

;**************************************************************************
;*
;* update
;*
;******
	align	4
update:
	jsr	doscroll

	lda	ylin_flag
	beq	ud_skp1
 	jsr	render_ylin	
	bra	ud_skp2
ud_skp1:
	jsr	render_ysin
	jsr	update_ysin_ptrs
ud_skp2:
	lda	xlin_flag
	beq	ud_skp3
 	jsr	render_xlin	
	bra	ud_ex1		

ud_skp3:
	jsr	render_xsin
	jsr	update_xsin_ptrs
ud_ex1:

	jsr	update_lums
	
	rts

	align	4
render_ylin:
; Y-positions
	lda	ylin_flag
	cmp	#2
	beq	ryl_skp4
		
	lda	#YSTEPS/2
	sta	tmp_zp
	lda	scr_pos
	lsr
	clc
	adc	#24
	ldy	#0	
	bra	ryl_skp5

ryl_skp4:
	lda	#YSTEPS
	sta	tmp_zp
	lda	scr_pos
	clc
	adc	#256-60
	ldy	#$ff

ryl_skp5:

; A=LSB, Y=MSB, tmp_zp=step
	ldx	#0
ryl_lp1:
	pha
	cpy	#0
	bne	ryl_skp1
	cmp	#208
	bcs	ryl_skp1
	dc.b	$2c
ryl_skp1:
	lda	#0
	sta	ypos,x
	pla
	clc
	adc	tmp_zp
	bcc	ryl_skp2
	iny
ryl_skp2:	
	inx
	cpx	#NUM_OBJECTS
	bne	ryl_lp1
	rts
	
	align	4
render_ysin:
; Y-positions
	lda	#>sin1st
	sta	ry_sm1+2
	lda	#>sin2st
	sta	ry_sm2+2

	ldx	#0
	ldy	scr_pos
ry_lp1:
	clc
ry_sm1:
	lda	sin1st,y
ry_sm2:
	adc	sin2st,y
	ror
	clc
	adc	#8
	sta	ypos,x
	tya
	clc
	adc	#STEPS
	tay
	bcc	ry_skp1
	inc	ry_sm1+2
	inc	ry_sm2+2
ry_skp1:
	inx
	cpx	#NUM_OBJECTS
	bne	ry_lp1
	rts

update_ysin_ptrs:
	inc	ry_sm1+1
	lda	ry_sm1+1
	cmp	#SIN1_LEN
	bne	ryp_skp1
	lda	#0
	sta	ry_sm1+1	
ryp_skp1:

	dec	ry_sm2+1
	lda	ry_sm2+1
	cmp	#255
	bne	ryp_skp2
	lda	#SIN2_LEN
	sta	ry_sm2+1	
ryp_skp2:
	rts

	align	4
render_xlin:
; X-positions
	ldx	#0
	lda	scr_pos
	ldy	#0
rxl_lp1:
	sta	xpos,x
	pha
	tya
	sta	xpos+1,x
	pla
	clc
	adc	#STEPS
	bcc	rxl_skp1
	iny
rxl_skp1:	
	inx
	inx
	cpx	#NUM_OBJECTS*2
	bne	rxl_lp1
	rts

	align	4
render_xsin:
; X-positions
	lda	sin3pos+1
	clc
	adc	#>sin3st
	sta	rx_sm1+2
	lda	sin3pos
	sta	rx_sm1+1
	lda	sin4pos+1
	clc
	adc	#>sin4st
	sta	rx_sm2+2
	lda	sin4pos
	sta	rx_sm2+1

	ldx	#0
	ldy	scr_pos
rx_lp1:
	clc
rx_sm1:
	lda	sin3st,y
rx_sm2:
	adc	sin4st,y
	ror
	clc
	adc	#[320-256]/2+8
	sta	xpos,x
	lda	#0
	adc	#0
	sta	xpos+1,x
	tya
	clc
	adc	#STEPS
	tay
	bcc	rx_skp1
	inc	rx_sm1+2
	inc	rx_sm2+2
rx_skp1:
	inx
	inx
	cpx	#NUM_OBJECTS*2
	bne	rx_lp1
	rts	

update_xsin_ptrs:
	inc	sin3pos
	bne	uxp_skp1
	inc	sin3pos+1
uxp_skp1:
	lda	sin3pos
	cmp	#<SIN3_LEN
	bne	uxp_skp2
	lda	sin3pos+1
	cmp	#>SIN3_LEN
	bne	uxp_skp2
	lda	#0
	sta	sin3pos	
	sta	sin3pos+1
uxp_skp2:

	lda	sin4pos
	bne	uxp_skp3
	dec	sin4pos+1
	bpl	uxp_skp3
	lda	#<SIN4_LEN
	sta	sin4pos
	lda	#>SIN4_LEN
	sta	sin4pos+1
	bra	uxp_skp4
uxp_skp3:	
	dec	sin4pos
uxp_skp4:
	rts

	
init_sin:
	lda	#0
	sta	xlin_flag
	sta	ylin_flag
	lda	#0
	sta	sin1pos
	sta	sin1pos+1
	sta	sin2pos
	sta	sin2pos+1
	sta	sin3pos
	sta	sin3pos+1
	sta	sin4pos
	sta	sin4pos+1
	ldx	#$ff
is_lp1:
	lda	sin1,x
	sta	sin1st+SIN1_LEN*2,x
	sta	sin1st+SIN1_LEN,x
	sta	sin1st,x
	lda	sin2,x
	sta	sin2st+SIN2_LEN*2,x
	sta	sin2st+SIN2_LEN,x
	sta	sin2st,x
	lda	sin3+$0100,x
	sta	sin3st+SIN3_LEN+$0100,x
	sta	sin3st+$0100,x
	lda	sin4+$0100,x
	sta	sin4st+SIN4_LEN+$0100,x
	sta	sin4st+$0100,x
	dex
	cpx	#$ff
	bne	is_lp1
is_lp2:
	lda	sin3,x
	sta	sin3st+SIN3_LEN,x
	sta	sin3st,x
	lda	sin4,x
	sta	sin4st+SIN4_LEN,x
	sta	sin4st,x
	dex
	cpx	#$ff
	bne	is_lp2
	rts

;**************************************************************************
;*
;* NAME  update_lums
;*
;******
init_lums:
	lda	#0
	sta	lum_count
	sta	lum_count2

	ldx	#LUMTAB2_LEN-1
ils_lp1:
	lda	lumtab2,x
	eor	#$0f
	sta	lumtab2_st+LUMTAB2_LEN*3,x
	sta	lumtab2_st+LUMTAB2_LEN*2,x
	sta	lumtab2_st+LUMTAB2_LEN*1,x
	sta	lumtab2_st+LUMTAB2_LEN*0,x
	dex
	bpl	ils_lp1
	rts
	
update_lums:

; music synced flash
	ldx	lum_count
	lda	lumtab,x
	eor	#$0f
	sta	curr_lum
	inx
	cpx	#LUMTAB_LEN
	bne	udl_skp1
	ldx	#0
udl_skp1:
	stx	lum_count

; determine mode to see what 
	lda	lum_mode
	beq	udl_set_lums
	cmp	#1
	bne	udl_set_lums2
	
	lda	curr_lum
udl_set_lums:
	ldx	#NUM_OBJECTS-1
udl_lp1:
	sta	lums,x
	dex
	bpl	udl_lp1
	rts

; modes 2 and up.
udl_set_lums2:

	lda	scr_corr
	beq	udl_skp3
	jsr	inc_lum2	
udl_skp3:
	lda	lum_mode
	cmp	#2
	bne	udl_skp4
	jsr	dec_lum2	
	bra	udl_skp5
udl_skp4:
	jsr	inc_lum2
udl_skp5:
	ldx	#0
	ldy	lum_count2
udl_lp2:
	lda	lumtab2_st,y
	sta	lums,x
	iny
	iny
	inx
	cpx	#NUM_OBJECTS
	bne	udl_lp2

	rts
dec_lum2:
	ldy	lum_count2
	dey
	bpl	dl2_skp1
	ldy	#LUMTAB2_LEN
dl2_skp1:
	sty	lum_count2
	rts
inc_lum2:
	ldy	lum_count2
	iny
	cpy	#LUMTAB2_LEN
	bne	il2_skp1
	ldy	#0
il2_skp1:
	sty	lum_count2
	rts

lumtab:
	dc.b	15
	dc.b	15,15,15,15,15,15
	
	dc.b	15,15,15,15,15,15
	dc.b	15,15,15,15,15,15
	dc.b	15,15,15,15,15,15
	dc.b	14,13,12,11,10,9
	dc.b	8,8,9,10,11,12
	dc.b	13,14,15,15,15,15
	dc.b	15,15,15,15,15
LUMTAB_LEN	equ	.-lumtab

	
lumtab2:
	ds.b	15,15
	dc.b	14,14,13,13,12,12,11,11,10,10,9,9
	dc.b	8,8,9,9,10,10,11,11,12,12
	dc.b	13,13,14,14,15,15,15,15
LUMTAB2_LEN	equ	.-lumtab2
	
	
;**************************************************************************
;*
;* NAME  doscroll
;*
;******
doscroll:
	dec	scr_pos
	bpl	ds_ex1

	lda	#STEPS-1
	sta	scr_pos
	sta	scr_corr
	ldx	#0
ud_lp3:
	lda	chars+1,x
	sta	chars,x
	lda	cols+1,x
	sta	cols,x
	inx
	cpx	#NUM_OBJECTS-1
	bne	ud_lp3

	dec	scr_cnt
	bne	ds_idle
	lda	scr_rate
	sta	scr_cnt
ds_lp1:
	jsr	get_char
	beq	ds_end
	bpl	ds_skp1
; Command
	cmp	#$81
	beq	ds_rate
	cmp	#$82
	beq	ds_rate
	cmp	#$83
	beq	ds_delay

ds_end:
	jsr	reset_scroll
	bra	ds_lp1

ds_rate:
	and	#$03
	sta	scr_rate
	sta	scr_cnt
	bra	ds_lp1
ds_delay:
	jsr	get_char
	sta	scr_cnt
	bra	ds_idle
			
ds_idle:
	lda	#" "

ds_skp1:
	sta	chars+NUM_OBJECTS-1

	ldx	cind
	lda	ctab,x
	eor	#$ff
	sta	cols+NUM_OBJECTS-1
	inx
	cpx	#CTAB_LEN
	bne	ds_skp3
	ldx	#0
ds_skp3:
	stx	cind
	rts		
ds_ex1:
	lda	#0
	sta	scr_corr
	rts	

cind:
	dc.b	0

ctab:
	dc.b	$1f,$1f,$2f,$2f,$3f,$3f,$4f,$4f,$5f,$5f,$6f,$6f,$7f,$7f
	dc.b	$8f,$8f,$9f,$9f,$af,$af,$bf,$bf,$cf,$cf,$df,$df,$ef,$ef
	dc.b	$ff,$ff	
CTAB_LEN	equ	.-ctab




get_char:
gc_sm1:
	lda	text
	php
	inc	gc_sm1+1
	bne	gc_ex1
	inc	gc_sm1+2
gc_ex1:
	plp
	rts

reset_scroll:
	lda	#<text
	sta	gc_sm1+1
	lda	#>text
	sta	gc_sm1+2
	rts

init_scroll:
	lda	#STEPS-1
	sta	scr_pos
	lda	#1
	sta	scr_cnt
	lda	#0
	sta	scr_corr
	sta	cind
	jsr	reset_scroll
	rts
	
;**************************************************************************
;*
;* initialize
;*
;******
init_text:
	jsr	init_scroll
	jsr	init_lums
		
	jsr	init_sin
			
	ldx	#0
it_lp1:
	txa
	asl
	tay
	lda	#" "
	sta	chars,x
	lda	#$f0
	sta	cols,x
	lda	#$00
	sta	lums,x
	lda	#100
	sta	ypos,x
	stx	tmp_zp
	lda	#0
	asl	tmp_zp
	rol
	asl	tmp_zp
	rol
	sta	xpos+1,y
	lda	tmp_zp
	sta	xpos,y
	lda	#SCREEN1_PAGE
	sta	d332_a,x
	sta	d332_b,x
	lda	#" "
	sta	d321_a,x
	sta	d321_b,x
	inx
	cpx	#NUM_OBJECTS
	bne	it_lp1

		
	lda	#<swap_tab1
	sta	swp_sm1+1
setup:
	ldx	#SWAP_TAB_LEN-1
swp_lp1:
	ldy	swap_tab3,x
swp_sm1:
	lda	swap_tab1,x
	sta	blit,y
	dex
	bpl	swp_lp1
	rts
swap:
	lda	swp_sm1+1
	eor	#SWAP_TAB_DIFF
	sta	swp_sm1+1
	bra	setup

		
swap_tab1:
	dc.b	>d321_a
	dc.b	>d330_a
	dc.b	>d331_a
	dc.b	>d332_a
	dc.b	>d321_a
	dc.b	>d330_a
	dc.b	>d331_a
	dc.b	>d332_a
SWAP_TAB_LEN	equ	.-swap_tab1
swap_tab2:
	dc.b	>d321_b
	dc.b	>d330_b
	dc.b	>d331_b
	dc.b	>d332_b
	dc.b	>d321_b
	dc.b	>d330_b
	dc.b	>d331_b
	dc.b	>d332_b
swap_tab3:
	dc.b	blt_sm1+2-blit
	dc.b	blt_sm2+2-blit
	dc.b	blt_sm3+2-blit
	dc.b	blt_sm4+2-blit
	dc.b	blt_sm5+2-blit
	dc.b	blt_sm6+2-blit
	dc.b	blt_sm7+2-blit
	dc.b	blt_sm8+2-blit

SWAP_TAB_DIFF	equ	[<swap_tab1]^[<swap_tab2]
	echo	"swap_tab1/swap_tab2 MSB:", >swap_tab1, >swap_tab2, "(must be equal)"


;**************************************************************************
;*
;* Check keys and set modes.
;*
;******
reset_mode:
	lda	#0
	sta	font_offs
	sta	lastkeystore
	sta	sine_mode
	sta	bg_mode
	sta	lum_mode
	jmp	update_mode
		
set_mode:
	jsr	keyscan

	lda	keydiff
	and	#%00000001
	beq	sm_skp1
	inc	sine_mode
	lda	sine_mode
	cmp	#SINE_MODE_MAX
	bne	sm_skp1
	lda	#0
	sta	sine_mode
sm_skp1:
	lda	keydiff
	and	#%00000010
	beq	sm_skp2
	clc
	lda	#$40
	adc	font_offs
	sta	font_offs
	cmp	#$c0
	bne	sm_skp2
	lda	#0
	sta	font_offs
sm_skp2:
	lda	keydiff
	and	#%00000100
	beq	sm_skp3
	inc	lum_mode
	lda	lum_mode
	cmp	#LUM_MODE_MAX
	bne	sm_skp3
	lda	#0
	sta	lum_mode
sm_skp3:
	lda	keydiff
	and	#%00001000
	beq	sm_skp4
	inc	bg_mode
	lda	bg_mode
	cmp	#BG_MODE_MAX
	bne	sm_skp4
	lda	#0
	sta	bg_mode
sm_skp4:

update_mode:
	lda	sine_mode
	asl
	tax
	lda	sine_mode_tab,x
	sta	xlin_flag
	lda	sine_mode_tab+1,x
	sta	ylin_flag

; set palette
	lda	bg_mode
	asl
	asl
	asl
	asl
	tay
	lda	btab,y
	sta	$d020
	ldx	#0
ivr_lp1:
	lda	btab,y
	sta	$d200,x
	iny
	inx
	cpx	#16
	bne	ivr_lp1

	rts

BG_MODE_MAX equ	7
btab:

	dc.b	$85,$85,$85,$85,$85,$85,$85,$85
	dc.b	$87,$87,$87,$87,$87,$87,$87,$87

	dc.b	$83,$83,$83,$83,$83,$83,$83,$83
	dc.b	$84,$84,$84,$84,$84,$84,$84,$84

	dc.b	$35,$35,$35,$35,$37,$37,$37,$37
	dc.b	$37,$37,$37,$37,$35,$35,$35,$35

	dc.b	$33,$33,$33,$33,$34,$34,$34,$34
	dc.b	$34,$34,$34,$34,$33,$33,$33,$33
	
	dc.b	$d4,$d4,$d6,$d6,$d6,$d6,$d4,$d4
	dc.b	$d4,$d4,$d6,$d6,$d6,$d6,$d4,$d4

	dc.b	$d3,$d3,$d4,$d4,$d4,$d4,$d3,$d3
	dc.b	$d3,$d3,$d4,$d4,$d4,$d4,$d3,$d3

	ds.b	16,$00
	if	0


	ds.b	16,$00
	endif

SINE_MODE_MAX equ	4
sine_mode_tab:
	dc.b	1,1
	dc.b	1,0
	dc.b	0,2
	dc.b	0,0
	
LUM_MODE_MAX equ	4

	
			
keyscan:
	ldx	#$ff
	stx	$dc02
	inx
	stx	$dc03
	ldy	#7
ks_lp1:
	lda	ks1tab,y
	sta	$dc00
	bit	$dc01		; It seems that without this,
				; scanning will break.  Burst mode?
	lda	$dc01
	and	ks2tab,y
	bne	ks_skp1
	txa
	ora	ks3tab,y
	tax
ks_skp1:
	dey
	bpl	ks_lp1
	stx	keystore
	txa
	eor	lastkeystore
	and	keystore
	sta	keydiff
	lda	keystore
	sta	lastkeystore
	rts
ks1tab:
	dc.b	%11111101
	dc.b	%11110111
	dc.b	%11111011
	dc.b	%11111011
	dc.b	%11111110
	dc.b	%11111110
	dc.b	%11111110
	dc.b	%11111110
ks2tab:
	dc.b	%00000100
	dc.b	%00010000
	dc.b	%00010000
	dc.b	%00000100
	dc.b	%00010000
	dc.b	%00000001
	dc.b	%00000010
	dc.b	%00000100
ks3tab:
	dc.b	%00000001
	dc.b	%00000010
	dc.b	%00000100
	dc.b	%00001000
	dc.b	%00000001
	dc.b	%00000010
	dc.b	%00000100
	dc.b	%00001000
	
	
; generated by makesinus.pl 0.0.2
; len=200, size=192
sin1:
        dc.b    $60,$63,$66,$69,$6c,$6f,$72,$75,$78,$7b,$7e,$81
        dc.b    $83,$86,$89,$8c,$8e,$91,$93,$96,$98,$9b,$9d,$9f
        dc.b    $a2,$a4,$a6,$a8,$aa,$ac,$ae,$af,$b1,$b3,$b4,$b6
        dc.b    $b7,$b8,$b9,$ba,$bb,$bc,$bd,$be,$be,$bf,$bf,$c0
        dc.b    $c0,$c0,$c0,$c0,$c0,$c0,$bf,$bf,$be,$be,$bd,$bc
        dc.b    $bb,$ba,$b9,$b8,$b7,$b6,$b4,$b3,$b1,$af,$ae,$ac
        dc.b    $aa,$a8,$a6,$a4,$a2,$9f,$9d,$9b,$98,$96,$93,$91
        dc.b    $8e,$8c,$89,$86,$83,$81,$7e,$7b,$78,$75,$72,$6f
        dc.b    $6c,$69,$66,$63,$60,$5d,$5a,$57,$54,$51,$4e,$4b
        dc.b    $48,$45,$42,$3f,$3d,$3a,$37,$34,$32,$2f,$2d,$2a
        dc.b    $28,$25,$23,$21,$1e,$1c,$1a,$18,$16,$14,$12,$11
        dc.b    $0f,$0d,$0c,$0a,$09,$08,$07,$06,$05,$04,$03,$02
        dc.b    $02,$01,$01,$00,$00,$00,$00,$00,$00,$00,$01,$01
        dc.b    $02,$02,$03,$04,$05,$06,$07,$08,$09,$0a,$0c,$0d
        dc.b    $0f,$11,$12,$14,$16,$18,$1a,$1c,$1e,$21,$23,$25
        dc.b    $28,$2a,$2d,$2f,$32,$34,$37,$3a,$3d,$3f,$42,$45
        dc.b    $48,$4b,$4e,$51,$54,$57,$5a,$5d
sin1end:
SIN1_LEN	equ	.-sin1

; generated by makesinus.pl 0.0.2
; len=187, size=192
sin2:
        dc.b    $60,$63,$66,$6a,$6d,$70,$73,$76,$79,$7d,$80,$83
        dc.b    $86,$89,$8c,$8e,$91,$94,$97,$99,$9c,$9e,$a1,$a3
        dc.b    $a5,$a7,$aa,$ac,$ae,$af,$b1,$b3,$b4,$b6,$b7,$b9
        dc.b    $ba,$bb,$bc,$bd,$be,$be,$bf,$bf,$c0,$c0,$c0,$c0
        dc.b    $c0,$c0,$bf,$bf,$bf,$be,$bd,$bc,$bb,$ba,$b9,$b8
        dc.b    $b7,$b5,$b4,$b2,$b0,$af,$ad,$ab,$a9,$a6,$a4,$a2
        dc.b    $9f,$9d,$9a,$98,$95,$93,$90,$8d,$8a,$87,$84,$81
        dc.b    $7e,$7b,$78,$75,$72,$6e,$6b,$68,$65,$62,$5e,$5b
        dc.b    $58,$55,$52,$4e,$4b,$48,$45,$42,$3f,$3c,$39,$36
        dc.b    $33,$30,$2d,$2b,$28,$26,$23,$21,$1e,$1c,$1a,$17
        dc.b    $15,$13,$11,$10,$0e,$0c,$0b,$09,$08,$07,$06,$05
        dc.b    $04,$03,$02,$01,$01,$01,$00,$00,$00,$00,$00,$00
        dc.b    $01,$01,$02,$02,$03,$04,$05,$06,$07,$09,$0a,$0c
        dc.b    $0d,$0f,$11,$12,$14,$16,$19,$1b,$1d,$1f,$22,$24
        dc.b    $27,$29,$2c,$2f,$32,$34,$37,$3a,$3d,$40,$43,$46
        dc.b    $4a,$4d,$50,$53,$56,$5a,$5d
sin2end:
SIN2_LEN	equ	.-sin2
; generated by makesinus.pl 0.0.2
; len=431, size=255
sin3:
        dc.b    $80,$81,$83,$85,$87,$89,$8b,$8c,$8e,$90,$92,$94
        dc.b    $96,$98,$99,$9b,$9d,$9f,$a1,$a2,$a4,$a6,$a8,$a9
        dc.b    $ab,$ad,$af,$b0,$b2,$b4,$b5,$b7,$b9,$ba,$bc,$be
        dc.b    $bf,$c1,$c3,$c4,$c6,$c7,$c9,$ca,$cc,$cd,$cf,$d0
        dc.b    $d2,$d3,$d4,$d6,$d7,$d8,$da,$db,$dc,$de,$df,$e0
        dc.b    $e1,$e3,$e4,$e5,$e6,$e7,$e8,$e9,$ea,$eb,$ec,$ed
        dc.b    $ee,$ef,$f0,$f1,$f2,$f2,$f3,$f4,$f5,$f5,$f6,$f7
        dc.b    $f7,$f8,$f9,$f9,$fa,$fa,$fb,$fb,$fc,$fc,$fc,$fd
        dc.b    $fd,$fd,$fe,$fe,$fe,$fe,$ff,$ff,$ff,$ff,$ff,$ff
        dc.b    $ff,$ff,$ff,$ff,$ff,$ff,$fe,$fe,$fe,$fe,$fe,$fd
        dc.b    $fd,$fd,$fc,$fc,$fb,$fb,$fb,$fa,$f9,$f9,$f8,$f8
        dc.b    $f7,$f6,$f6,$f5,$f4,$f4,$f3,$f2,$f1,$f0,$ef,$ef
        dc.b    $ee,$ed,$ec,$eb,$ea,$e9,$e8,$e6,$e5,$e4,$e3,$e2
        dc.b    $e1,$e0,$de,$dd,$dc,$da,$d9,$d8,$d6,$d5,$d4,$d2
        dc.b    $d1,$cf,$ce,$cd,$cb,$ca,$c8,$c6,$c5,$c3,$c2,$c0
        dc.b    $bf,$bd,$bb,$ba,$b8,$b6,$b5,$b3,$b1,$b0,$ae,$ac
        dc.b    $aa,$a9,$a7,$a5,$a3,$a1,$a0,$9e,$9c,$9a,$98,$97
        dc.b    $95,$93,$91,$8f,$8d,$8c,$8a,$88,$86,$84,$82,$80
        dc.b    $7f,$7d,$7b,$79,$77,$75,$73,$72,$70,$6e,$6c,$6a
        dc.b    $68,$67,$65,$63,$61,$5f,$5e,$5c,$5a,$58,$56,$55
        dc.b    $53,$51,$4f,$4e,$4c,$4a,$49,$47,$45,$44,$42,$40
        dc.b    $3f,$3d,$3c,$3a,$39,$37,$35,$34,$32,$31,$30,$2e
        dc.b    $2d,$2b,$2a,$29,$27,$26,$25,$23,$22,$21,$1f,$1e
        dc.b    $1d,$1c,$1b,$1a,$19,$17,$16,$15,$14,$13,$12,$11
        dc.b    $10,$10,$0f,$0e,$0d,$0c,$0b,$0b,$0a,$09,$09,$08
        dc.b    $07,$07,$06,$06,$05,$04,$04,$04,$03,$03,$02,$02
        dc.b    $02,$01,$01,$01,$01,$01,$00,$00,$00,$00,$00,$00
        dc.b    $00,$00,$00,$00,$00,$00,$01,$01,$01,$01,$02,$02
        dc.b    $02,$03,$03,$03,$04,$04,$05,$05,$06,$06,$07,$08
        dc.b    $08,$09,$0a,$0a,$0b,$0c,$0d,$0d,$0e,$0f,$10,$11
        dc.b    $12,$13,$14,$15,$16,$17,$18,$19,$1a,$1b,$1c,$1e
        dc.b    $1f,$20,$21,$23,$24,$25,$26,$28,$29,$2b,$2c,$2d
        dc.b    $2f,$30,$32,$33,$35,$36,$38,$39,$3b,$3c,$3e,$40
        dc.b    $41,$43,$44,$46,$48,$49,$4b,$4d,$4f,$50,$52,$54
        dc.b    $56,$57,$59,$5b,$5d,$5e,$60,$62,$64,$66,$67,$69
        dc.b    $6b,$6d,$6f,$71,$72,$74,$76,$78,$7a,$7c,$7e
sin3end:
SIN3_LEN	equ	.-sin3
; generated by makesinus.pl 0.0.2
; len=462, size=255
sin4:
        dc.b    $80,$81,$83,$85,$86,$88,$8a,$8c,$8d,$8f,$91,$93
        dc.b    $94,$96,$98,$99,$9b,$9d,$9e,$a0,$a2,$a3,$a5,$a7
        dc.b    $a8,$aa,$ac,$ad,$af,$b0,$b2,$b4,$b5,$b7,$b8,$ba
        dc.b    $bb,$bd,$be,$c0,$c1,$c3,$c4,$c6,$c7,$c9,$ca,$cc
        dc.b    $cd,$ce,$d0,$d1,$d2,$d4,$d5,$d6,$d7,$d9,$da,$db
        dc.b    $dc,$de,$df,$e0,$e1,$e2,$e3,$e4,$e5,$e6,$e7,$e8
        dc.b    $e9,$ea,$eb,$ec,$ed,$ee,$ef,$f0,$f0,$f1,$f2,$f3
        dc.b    $f3,$f4,$f5,$f6,$f6,$f7,$f7,$f8,$f9,$f9,$fa,$fa
        dc.b    $fb,$fb,$fb,$fc,$fc,$fd,$fd,$fd,$fd,$fe,$fe,$fe
        dc.b    $fe,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff
        dc.b    $ff,$ff,$ff,$fe,$fe,$fe,$fe,$fd,$fd,$fd,$fd,$fc
        dc.b    $fc,$fb,$fb,$fb,$fa,$fa,$f9,$f9,$f8,$f7,$f7,$f6
        dc.b    $f6,$f5,$f4,$f3,$f3,$f2,$f1,$f0,$f0,$ef,$ee,$ed
        dc.b    $ec,$eb,$ea,$e9,$e8,$e7,$e6,$e5,$e4,$e3,$e2,$e1
        dc.b    $e0,$df,$de,$dc,$db,$da,$d9,$d7,$d6,$d5,$d4,$d2
        dc.b    $d1,$d0,$ce,$cd,$cc,$ca,$c9,$c7,$c6,$c4,$c3,$c1
        dc.b    $c0,$bf,$bd,$bb,$ba,$b8,$b7,$b5,$b4,$b2,$b1,$af
        dc.b    $ad,$ac,$aa,$a8,$a7,$a5,$a3,$a2,$a0,$9e,$9d,$9b
        dc.b    $99,$98,$96,$94,$93,$91,$8f,$8d,$8c,$8a,$88,$86
        dc.b    $85,$83,$81,$80,$7e,$7c,$7a,$79,$77,$75,$73,$72
        dc.b    $70,$6e,$6d,$6b,$69,$67,$66,$64,$62,$61,$5f,$5d
        dc.b    $5c,$5a,$58,$57,$55,$53,$52,$50,$4f,$4d,$4b,$4a
        dc.b    $48,$47,$45,$44,$42,$41,$3f,$3e,$3c,$3b,$39,$38
        dc.b    $36,$35,$33,$32,$31,$2f,$2e,$2d,$2b,$2a,$29,$28
        dc.b    $26,$25,$24,$23,$21,$20,$1f,$1e,$1d,$1c,$1b,$1a
        dc.b    $19,$18,$17,$16,$15,$14,$13,$12,$11,$10,$0f,$0f
        dc.b    $0e,$0d,$0c,$0c,$0b,$0a,$09,$09,$08,$08,$07,$06
        dc.b    $06,$05,$05,$04,$04,$04,$03,$03,$02,$02,$02,$02
        dc.b    $01,$01,$01,$01,$00,$00,$00,$00,$00,$00,$00,$00
        dc.b    $00,$00,$00,$00,$00,$00,$01,$01,$01,$01,$02,$02
        dc.b    $02,$02,$03,$03,$04,$04,$04,$05,$05,$06,$06,$07
        dc.b    $08,$08,$09,$09,$0a,$0b,$0c,$0c,$0d,$0e,$0f,$0f
        dc.b    $10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$1a,$1b
        dc.b    $1c,$1d,$1e,$1f,$20,$21,$23,$24,$25,$26,$27,$29
        dc.b    $2a,$2b,$2d,$2e,$2f,$31,$32,$33,$35,$36,$38,$39
        dc.b    $3b,$3c,$3d,$3f,$40,$42,$44,$45,$47,$48,$4a,$4b
        dc.b    $4d,$4e,$50,$52,$53,$55,$57,$58,$5a,$5c,$5d,$5f
        dc.b    $61,$62,$64,$66,$67,$69,$6b,$6c,$6e,$70,$72,$73
        dc.b    $75,$77,$79,$7a,$7c,$7e
sin4end:
SIN4_LEN	equ	.-sin4

;**************************************************************************
;*
;* intro
;*
;******
intro_init:
	jsr	$ff5b
	sei
	lda	#0
	sta	$d020
	jsr	wait_wb
	lda	#$0b
	sta	$d011
	lda	#$8 | 4
	sta	$d016
	lda	#147
	jsr	$ffd2
	lda	#14
	jsr	$ffd2
	lda	#<msg
	ldy	#>msg
	jmp	print
	
intro_run:
	sei
	jsr	wait_wb
	lda	#$15
	sta	$d018
	lda	#$1f
	sta	$d011
	lda	#$00
	sta	$d208
	lda	#6
	sta	$d020
	lda	#0
	sta	$d021
	sta	$dc03
	lda	#1
	jsr	SONG_DEST
	lda	#6
	sta	tmp_zp
int_r_lp1:
	jsr	SONG_DEST+3
	dec	tmp_zp
	bne	int_r_lp1
	
	ldy	#0
	sty	ctab2_cnt
	sty	ctab3_cnt
int_lp3:
	jsr	wait_wb
	ldx	ctab3_cnt
	lda	ctab3_1,x
	sta	$d206
	lda	ctab3_2,x
	sta	$d20e
	ldy	ctab2_cnt
	lda	ctab2,y
	sta     $d208
	iny
	cpy	#CTAB2_LEN
	bne	int_skp1
	ldy	#CTAB2_RPT
int_skp1:
	sty	ctab2_cnt
	jsr	SONG_DEST+3

	lda	SONG_DEST+$3f
	cmp	#$11
	bne	int_skp2
	lda	#0
	sta	SONG_DEST+$3f
	dec	ctab3_cnt
	bpl	int_skp2
	lda	#CTAB3_LEN-1
	sta	ctab3_cnt
int_skp2:
	
	ldx	#$00
	stx	$dc02
	bit	$dc00		; burst mode
	lda	$dc00
	and	#%00010000
	beq	int_ex1
	dex
	stx	$dc02
	lda	#%01111111
	sta	$dc00
	bit	$dc01		; burst mode
	lda	$dc01
	and	#%00010000
	bne	int_lp3

int_ex1:
	lda	#$00
	sta	$d418
	sta	$d404
	sta	$d40b
	sta	$d412
	sta	$d020
	lda	#$0b
	sta	$d011
	rts

ctab2:
	ds.b	10,$0f
CTAB2_RPT	equ	.-ctab2
	dc.b	$0f,$0e,$0d,$0c,$0b,$0a,$09,$08,$07,$06,$05
	dc.b	$04,$05,$06,$07,$08,$09,$0a,$0b,$0c,$0d,$0e
CTAB2_LEN	equ	.-ctab2
msg:
	incbin	"data/blitter.bin"
ctab3_1:
	dc.b	$86,$db,$36
CTAB3_LEN	equ	.-ctab3_1
ctab3_2:
	dc.b	$9a,$df,$3b
;**************************************************************************
;*
;* scroller
;*   
;******
text:
	incbin	"data/scrtext.bin"
;**************************************************************************
;*
;* NAME  print
;*
;* DESCRIPTION
;*   output string
;*   
;******
print:
	sta	ptr_zp
	sty	ptr_zp+1
	ldy	#0
prt_lp1:
	lda	(ptr_zp),y
	beq	prt_ex1
	jsr	$ffd2
	iny
	bne	prt_lp1
	inc	ptr_zp+1
	jmp	prt_lp1
prt_ex1:
	rts

	
	if	CUSTOM_FONT
font_st:
	incbin	"data/font.bin"
font_end:
	endif


	echo	"end code", .	
code_end	equ	.	

;**************************************************************************
;*
;* discardable data
;*
;******
SONG_DEST	equ	$f000

DATA_DEST	equ	SONG_DEST
install:
	ldx	#$00
ist_lp1:
ist_sm1:
	lda	data_st,x
ist_sm2:
	sta	DATA_DEST,x
	inx
	bne	ist_lp1
	inc	ist_sm1+2
	inc	ist_sm2+2
	lda	ist_sm1+2
	cmp	#[>data_end]+1
	bne	ist_lp1
	rts

data_st:
sng_st:
	incbin	"data/blitter_sng.bin"
sng_end:
data_end:

;**************************************************************************
;*
;* bss
;*
;******
; generate timing code
	seg.u	bss
	org	code_end
ctab2_cnt:
	ds.b	1
ctab3_cnt:
	ds.b	1
buf_disp:
	ds.b	1
curr_tab:
	ds.b	1
scr_pos:
	ds.b	1
scr_cnt:
	ds.b	1
scr_corr:
	ds.b	1
scr_rate:
	ds.b	1
sin1pos:
	ds.w	1
sin2pos:
	ds.w	1
sin3pos:
	ds.w	1
sin4pos:
	ds.w	1
xlin_flag:
	ds.b	1
ylin_flag:
	ds.b	1
sine_mode:
	ds.b	1
bg_mode:
	ds.b	1
lum_mode:
	ds.b	1
lum_count:
	ds.b	1
lum_count2:
	ds.b	1
curr_lum:
	ds.b	1
keystore:
	ds.b	1
lastkeystore:
	ds.b	1
keydiff:
	ds.b	1
	align	256
lumtab2_st:
	ds.b	LUMTAB2_LEN*4
	align	256
font_temp:
	ds.b	64*4
	align	256
render_buf:
	ds.b	256*3
	align	256
chars:
	ds.b	NUM_OBJECTS
cols:
	ds.b	NUM_OBJECTS
lums:
	ds.b	NUM_OBJECTS
xpos:
	ds.w	NUM_OBJECTS
ypos:
	ds.b	NUM_OBJECTS

	align	256
sin1st:
	ds.b	256*3
	align	256
sin2st:
	ds.b	256*3
	align	256	
sin3st:
	ds.b	256*4
	align	256	
sin4st:
	ds.b	256*4
	align	256	
d321_a:
	ds.b	NUM_OBJECTS
d330_a:
	ds.b	NUM_OBJECTS
d331_a:
	ds.b	NUM_OBJECTS
d332_a:
	ds.b	NUM_OBJECTS
	align	256	
d321_b:
	ds.b	NUM_OBJECTS
d330_b:
	ds.b	NUM_OBJECTS
d331_b:
	ds.b	NUM_OBJECTS
d332_b:
	ds.b	NUM_OBJECTS

	
	echo	"end bss", .	
; eof
